package node.block.stmt;

import midcode.MidCode;
import midcode.MidcodeGenerator;
import midcode.OPType;
import node.Node;
import node.exp.Exp;
import node.exp.LVal;
import symbol.Symbol;

public class GetintStmt implements Node {
    private final LVal lVal;

    public GetintStmt(LVal lVal) {
        this.lVal = lVal;
    }

    @Override
    public Node getFather() {
        return father;
    }

    @Override
    public String genMidCode() {
        MidcodeGenerator mg = MidcodeGenerator.getInstance();
        String tmp = mg.genTmp();
        mg.addMidcode(new MidCode(OPType.GETINT, null, null, tmp));
        Symbol symbol = lVal.symbol;
        Exp index_1 = lVal.index_1;
        Exp index_2 = lVal.index_2;
        if (symbol.dim == 0) {
            mg.addMidcode(new MidCode(OPType.ASSIGN, symbol.getName(), null, tmp));
        } else if (symbol.dim == 1) {
            assert index_1 != null;
            mg.addMidcode(new MidCode(OPType.SW_ARRAY, symbol.getName(), index_1.genMidCode(), tmp));
        } else {
            assert index_2 != null;
            mg.genArrayIndex(symbol, index_1.genMidCode(), index_2.genMidCode(), tmp);
        }
        return "";
    }
}

